package mainpackage;

import engine.ani.Player;
import engine.net.NetManager;
import function.NPCTalk;

import java.io.DataInputStream;
import javax.microedition.lcdui.*;

import ui.UiEquipment;
import ui.UiInterFace;
import ui.UiLogin;
import ui.UiPackage;

import java.util.HashMap;
import java.util.Vector;
//import com.nokia.mid.ui.*;

/**
 * <p>Title: 游戏画布类实现游戏主线程和paint方法</p>
 * <p>Description: 主要负责游戏的按键，绘制，调试等操作</p>
 * <p>Copyright: Copyright (c) 2010</p>
 * <p>Company: </p>
 * @author hlw
 * @version 1.0
 */
public class MainCanvas extends Canvas implements Runnable {
    // 按键处理支持变量

    private static int iKeyState;
    private static int iKeyPressed;
    private static int iKeyReleased;
    public static int KEY = -100;
    public static int KEY_BOTTOM = -100;
    private static int tempKey = -100;
    // 调试信息
    private long lastSystemTime;
    public static int fps = 12;
    private int additiveFrameCount;
    private Runtime rt = Runtime.getRuntime();
//    private static boolean printStackToScreen = false;
    private static Vector callStack, logInfo;
    private static int screenInfoPosX, screenInfoPosY;
    //网络消息接收条数
    public static int iReceiveCounter;
    // 帧间隔计算
    public static long lStartTime;
    private long lTimeTaken;
    private long lTimegc;
    // 游戏线程运行开关
    public static boolean blStopGame;
    // 更新与绘制的同步控制变量
    private boolean bCompletePaint;
    // 画笔
    public static Graphics backGraphics;
    // 背景缓冲
    public static Image backImage;
    // Logic实例
    private static MainLogic logic;
    //游戏显示类
    public static MainView mainView;
    //游戏主线程
    private Thread mainThread;
    public static boolean bInCanvas = true;
    /**
     * 初始化游戏
     */
    public MainCanvas() {
        setFullScreenMode(true);
        if (Constant.DEBUG) {
            rt = Runtime.getRuntime();
        }
            // 生成背景缓冲区
        //#if NOKIA_S40V3 || NORMAL_240_400 || MOT0 || K790
        //#else
        this.backImage = Image.createImage(Constant.SCREEN_WIDTH, Constant.SCREEN_HEIGHT);
        this.backGraphics = this.backImage.getGraphics();
        this.backGraphics.setFont(Constant.FONT_SMALL);
        //#endif
        this.mainView = new MainView();
        logic = MainLogic.getInstance();
        this.blStopGame = false;
         //
//        MainLogic.gameMap.initMapData(new DataInputStream(this.getClass().getResourceAsStream("/res/S60V3/xinshoucun 1 0.ter")), false);
    }

    /**
     * 启动游戏主线程
     */
    public void start() {
        if (Constant.DEBUG) {
            this.addLog("启动主线程.");
        }
        if (this.mainThread == null || (this.mainThread != null && !this.mainThread.isAlive())) {
            this.mainThread = new Thread(this);
            this.mainThread.start();
        }
    }

    /**
     * 清理资源
     */
    public void clearRes() {
        this.backGraphics = null;
        this.backImage = null;
        this.logic = null;
        MainLogic.logic = null;
        System.gc();
    }
    /**
     * 更新游戏
     */
    //开始时间
    public static long lstarTime = System.currentTimeMillis();
    //回收间隔时间
    public static long lconTime = 15000;
    public static long lLastDate = -1;

    public void updateGame() {
//        if (System.currentTimeMillis() - this.lstarTime > this.lconTime) {
//            System.gc();
//            this.lstarTime = System.currentTimeMillis();
//        }
        //游戏逻辑更新
//        lLastDate = System.currentTimeMillis();
        this.logic.update();
//        System.out.println("游戏更新执行时间:" + (System.currentTimeMillis() - lLastDate));
        this.backGraphics.setColor(0xffffff);
        this.backGraphics.fillRect(0, 0, Constant.SCREEN_WIDTH, Constant.SCREEN_HEIGHT);
        //游戏绘制
//        lLastDate = System.currentTimeMillis();
        this.mainView.draw(this.logic.byGameState, this.backGraphics);
//        System.out.println("游戏绘制执行时间:" + (System.currentTimeMillis() - lLastDate));

//        if (!this.bCompletePaint) {
//            return;
//        }

        // 更新结束，paint()可以绘制了
        this.bCompletePaint = false;
        this.repaint();
        this.serviceRepaints();
    }

    /**
     * 绘制游戏
     * @param g Graphics 画笔
     */
    protected void paint(Graphics g) {
//        if (Constant.DEBUG) {
//            if (printStackToScreen) {
//                printLogAndStack(g);
//                return;
//            }
//        }
        // 没有更新结束不绘制
//        if (this.bCompletePaint) {
//            return;
//        }
        if (Constant.DEBUG) {
            this.pushCallStack("Logic.paint()");
        }

        //#if NOKIA_S40V3 || NORMAL_240_400 || MOT0 || K790
//#         this.backGraphics = g;
//#         this.backGraphics.setFont(Constant.FONT_SMALL);
//#         drawBenchMark(g);
        //#else
        g.drawImage(this.backImage, 0, 0, Graphics.LEFT | Graphics.TOP);
        //#endif
//	g.setColor(0xffffff);
//	g.fillRect(0, 0 , Constant.SCREEN_WIDTH, Constant.SCREEN_HEIGHT);
        // 绘制游戏数据
        drawBenchMark(g);
        // 绘制结束，updateGame()可以更新了
//        if (Constant.DEBUG) {

//        }
        this.bCompletePaint = true;
        if (Constant.DEBUG) {
            this.popCallStack();
        }
    }

    /**
     * 映射键值获得方法（功能：根据物理键值获得映射键值）
     * @param keyCode int 物理键值
     * @return int 映射键值
     */
    private final int getKeyValue(int keyCode) {
        if (keyCode == Constant.KEY_CODE_SOFT_LEFT) {
            return Constant.KEY_SOFT_LEFT;
        } else if (keyCode == Constant.KEY_CODE_SOFT_RIGHT) {
            return Constant.KEY_SOFT_RIGHT;
        } else if (keyCode == Canvas.KEY_NUM0) {
            return Constant.KEY_0;
        } else if (keyCode == Canvas.KEY_NUM1) {
            return Constant.KEY_1;
        } else if (keyCode == Canvas.KEY_NUM2) {
            return Constant.KEY_2;
        } else if (keyCode == Canvas.KEY_NUM3) {
            return Constant.KEY_3;
        } else if (keyCode == Canvas.KEY_NUM4) {
            return Constant.KEY_4;
        } else if (keyCode == Canvas.KEY_NUM5) {
            return Constant.KEY_5;
        } else if (keyCode == Canvas.KEY_NUM6) {
            return Constant.KEY_6;
        } else if (keyCode == Canvas.KEY_NUM7) {
            return Constant.KEY_7;
        } else if (keyCode == Canvas.KEY_NUM8) {
            return Constant.KEY_8;
        } else if (keyCode == Canvas.KEY_NUM9) {
            return Constant.KEY_9;
        } else if (keyCode == Canvas.KEY_POUND) {
            return Constant.KEY_POUND;
        } else if (keyCode == Canvas.KEY_STAR) {
            return Constant.KEY_STAR;
        } //#if NOKIA_E72
        else if (keyCode == 109) {
            return Constant.KEY_0;
        } else if (keyCode == 114) {
            return Constant.KEY_1;
        } else if (keyCode == 116) {
            return Constant.KEY_2;
        } else if (keyCode == 121) {
            return Constant.KEY_3;
        } else if (keyCode == 102) {
            return Constant.KEY_4;
        } else if (keyCode == 103) {
            return Constant.KEY_5;
        } else if (keyCode == 104) {
            return Constant.KEY_6;
        } else if (keyCode == 118) {
            return Constant.KEY_7;
        } else if (keyCode == 98) {
            return Constant.KEY_8;
        } else if (keyCode == 110) {
            return Constant.KEY_9;
        } else if (keyCode == 106) {
            return Constant.KEY_POUND;
        } else if (keyCode == 117) {
            return Constant.KEY_STAR;
        } //#endif
        else if (keyCode == Constant.KEY_C_) {

            return Constant.KEY_C;
        } // 如果满足此条件，则必是导航键，而非数字键
        else if (this.getGameAction(keyCode) == Canvas.LEFT) {
            return Constant.KEY_DIR_LEFT;
        } else if (this.getGameAction(keyCode) == Canvas.RIGHT) {
            return Constant.KEY_DIR_RIGHT;
        } else if (this.getGameAction(keyCode) == Canvas.UP) {
            return Constant.KEY_DIR_UP;
        } else if (this.getGameAction(keyCode) == Canvas.DOWN) {
            return Constant.KEY_DIR_DOWN;
        } else if (this.getGameAction(keyCode) == Canvas.FIRE) {
            return Constant.KEY_DIR_CENTER;
        } else {
            return Constant.KEY_UNDEFINED;
        }
    }

    /**
     * 系统的键按下判断方法, 系统方法每按一下响应一次
     * @param keyCode int 当前按键键值
     */
//    public static int key111;
    protected final void keyPressed(int keyCode) {
//        MainCanvas.clearInput();
        keyReleased(keyCode);
//        key111 = keyCode;
        int keyValue = this.getKeyValue(keyCode);
        //#if NOKIA_S60V5
         switch (MainLogic.getInstance().byGameState) {
             case Constant.GAME_UI_ROAM_MENU:
             case Constant.GAME_UI_TONG_MENU:
             case Constant.GAME_UI_NPC_TALK:
             case Constant.GAME_UI_EQUIP_REPAIR:
             case Constant.GAME_UI_TASK_AUTO_FIND_PATH:
             case Constant.GAME_UI_TEAM_ANSWER:
             case Constant.GAME_UI_WAREHOUSE:
             case Constant.GAME_UI_REVIVE:
             case Constant.GAME_UI_VIEW_OTHER_PLAYER:
             case Constant.GAME_UI_MAP_WORLD:
                 if (keyValue == Constant.KEY_DIR_UP) {
                     KEY_BOTTOM = Constant.UP;
                 } else if (keyValue == Constant.KEY_DIR_DOWN) {
                     KEY_BOTTOM = Constant.DOWN;
                 } else if (keyValue == Constant.KEY_DIR_LEFT) {
                     KEY_BOTTOM = Constant.LEFT;
                 } else if (keyValue == Constant.KEY_DIR_RIGHT) {
                     KEY_BOTTOM = Constant.RIGHT;
                 }
                 break;
             case Constant.GAME_UI_OPTION_LIST:
                 if (keyValue == Constant.KEY_DIR_CENTER) {
                     KEY = Constant.FIRE;
                 }
                 break;
             default:
                 if (keyValue == Constant.KEY_DIR_UP) {
                     KEY = Constant.UP;
                 } else if (keyValue == Constant.KEY_DIR_DOWN) {
                     KEY = Constant.DOWN;
                 } else if (keyValue == Constant.KEY_DIR_LEFT) {
                     KEY = Constant.LEFT;
                 } else if (keyValue == Constant.KEY_DIR_RIGHT) {
                     KEY = Constant.RIGHT;
                 } else if (keyValue == Constant.KEY_DIR_CENTER) {
                     KEY = Constant.FIRE;
                 }
                 break;
         }
        //#endif
        // 不处理未定义的按键
        if (keyValue == Constant.KEY_UNDEFINED) {
            return;
        }
        this.iKeyState = this.iKeyState | keyValue;
        this.iKeyReleased = this.iKeyReleased & ~keyValue;
        this.iKeyPressed = this.iKeyPressed | keyValue;
    }

    public static void keyPressed2(int keyCode) {
        tempKey = keyCode;
        int keyValue = keyCode;
        // 不处理未定义的按键
        if (keyValue == Constant.KEY_UNDEFINED) {
            return;
        }
        iKeyState = iKeyState | keyValue;
        iKeyReleased = iKeyReleased & ~keyValue;
        iKeyPressed = iKeyPressed | keyValue;
    }

    public final void setKey(int keyCode) {
        clearInput();
        int keyValue = this.getKeyValue(keyCode);
        // 不处理未定义的按键
        if (keyValue == Constant.KEY_UNDEFINED) {
            return;
        }
        this.iKeyState = this.iKeyState | keyValue;
        this.iKeyReleased = this.iKeyReleased & ~keyValue;
        this.iKeyPressed = this.iKeyPressed | keyValue;
    }

    /**
     * 系统的键抬起判断方法
     * @param keyCode int 当前释放的按键键值
     */
    protected final void keyReleased(int keyCode) {
        int keyValue = this.getKeyValue(keyCode);
        KEY = -100;
        KEY_BOTTOM = -100;
        // 不处理未定义的按键
        if (keyValue == Constant.KEY_UNDEFINED || this.iKeyState == 0) {
            return;
        }
        this.iKeyReleased = this.iKeyReleased | keyValue;
    }

    public void keyReleased2(int keyCode) {
        int keyValue = keyCode;
        // 不处理未定义的按键
        if (keyValue == Constant.KEY_UNDEFINED || this.iKeyState == 0) {
            return;
        }
        this.iKeyReleased = this.iKeyReleased | keyValue;
    }

    /**
     * 功能判断映射键值取得方法（功能:将漫游或单次按键键值映射到指定变量）
     * @param keyState int 按下键的映射键值
     * @return int 用来功能判断的映射键值
     */
    private final static int getGameKey(int keyState) {
        int gameKeyState = Constant.GAME_KEY_UNDEFINED;
        if ((keyState & Constant.KEY_DIR_LEFT) != 0 /*||
                (keyState & Constant.KEY_4) != 0*/) {
            gameKeyState |= Constant.GAME_KEY_LEFT; // LEFT
        }
        if ((keyState & Constant.KEY_DIR_RIGHT) != 0 /*||
                (keyState & Constant.KEY_6) != 0*/) {
            gameKeyState |= Constant.GAME_KEY_RIGHT; // RIGHT
        }
        if ((keyState & Constant.KEY_DIR_UP) != 0 /*||
                (keyState & Constant.KEY_2) != 0*/) {
            gameKeyState |= Constant.GAME_KEY_UP; // UP
        }
        if ((keyState & Constant.KEY_DIR_DOWN) != 0 /*||
                (keyState & Constant.KEY_8) != 0*/) {
            gameKeyState |= Constant.GAME_KEY_DOWN; // DOWN
        }
        if ((keyState & Constant.KEY_DIR_CENTER) != 0 /*||
                (keyState & Constant.KEY_5) != 0*/) {
            gameKeyState |= Constant.GAME_KEY_FIRE; // FIRE
        }
        if ((keyState & Constant.KEY_SOFT_LEFT) != 0) {
            gameKeyState |= Constant.GAME_KEY_POSITIVE; // LEFT SOFT
        }
        if ((keyState & Constant.KEY_SOFT_RIGHT) != 0) {
            gameKeyState |= Constant.GAME_KEY_NEGATIVE; // RIGHT SOFT
        }
        if ((keyState & Constant.KEY_0) != 0) {
            gameKeyState |= Constant.GAME_KEY_0;
        }
        if ((keyState & Constant.KEY_1) != 0) {
            gameKeyState |= Constant.GAME_KEY_1;
        }
        if ((keyState & Constant.KEY_2) != 0) {
            gameKeyState |= Constant.GAME_KEY_2;
        }
        if ((keyState & Constant.KEY_3) != 0) {
            gameKeyState |= Constant.GAME_KEY_3;
        }
        if ((keyState & Constant.KEY_4) != 0) {
            gameKeyState |= Constant.GAME_KEY_4;
        }
        if ((keyState & Constant.KEY_5) != 0) {
            gameKeyState |= Constant.GAME_KEY_5;
        }
        if ((keyState & Constant.KEY_6) != 0) {
            gameKeyState |= Constant.GAME_KEY_6;
        }
        if ((keyState & Constant.KEY_7) != 0) {
            gameKeyState |= Constant.GAME_KEY_7;
        }
        if ((keyState & Constant.KEY_8) != 0) {
            gameKeyState |= Constant.GAME_KEY_8;
        }
        if ((keyState & Constant.KEY_9) != 0) {
            gameKeyState |= Constant.GAME_KEY_9;
        }
        if ((keyState & Constant.KEY_STAR) != 0) {
            gameKeyState |= Constant.GAME_KEY_STAR;
        }
        if ((keyState & Constant.KEY_POUND) != 0) {
            gameKeyState |= Constant.GAME_KEY_POUND;
        }
        if ((keyState & Constant.KEY_C) != 0) {
            gameKeyState |= Constant.GAME_KEY_C;
        }
        return gameKeyState;
    }

    /**
     * 按键逻辑判断方法（功能：根据不同的按键响应不同的功能）
     * @param keyState int 功能判断的映射键值
     * @param rambleOn boolean 连续响应按键开关
     * @return boolean 按下键的确认开关
     */
    public final static boolean keyPressedLogic(int keyState, boolean rambleOn) {
        int key = rambleOn ? iKeyState : iKeyPressed;
        if (rambleOn) {
            iKeyState = iKeyState ^ iKeyReleased;
            iKeyReleased = 0;
        }
        if ((getGameKey(key) & keyState) != 0) {
            if (!rambleOn) {
                iKeyPressed = 0;
            }
            return true;
        } else {
            return false;
        }
    }

    /**
     * 清理按键缓冲
     */
    public final static void clearInput() {
        iKeyState = 0;
        iKeyPressed = 0;
        iKeyReleased = 0;
    }
    //#if NOKIA_S60V5
     /**
      * 触摸按键
      */
     static private int pointerX;// 鼠标即时坐标X
     static private int pointerY;// 鼠标即时坐标Y
     static public int curPointerX;// 鼠标坐标X快照
     static public int curPointerY;// 鼠标坐标Y快照
     static public int onPointer = -100;

     /**
      *
      * @param rectX  触摸起始点X
      * @param rectY  触摸起始点Y
      * @param rectWidth  触摸区域宽
      * @param rectHeight 触摸区域高
      * @param rambleOn   是否按住
      * @return
      */
     public static boolean pointerPressedLogic(int rectX, int rectY, int rectWidth, int rectHeight, boolean rambleOn) {
         if (onPointer > -1) {
             if (pointerX >= rectX
                     && pointerX <= (rectX + rectWidth)
                     && pointerY >= rectY
                     && pointerY <= (rectY + rectHeight)) {
                 if (!rambleOn) {
                     onPointer = -100;
                 }
                 return true;
             } else {
                 return false;
             }
         } else {
             return false;
         }
     }

     public static void clearpointerPressedKey() {
         KEY = -100;
         KEY_BOTTOM = -100;
     }
     public static int touch_y;
     private boolean blDragged = false;
     private int iDraggedIndex = -1;

     protected void pointerDragged(int x, int y) {
         pointerX = x;
         pointerY = y;
         if (iDraggedIndex != -1) {
             iDraggedIndex += 1;
         }
         if (tempKey != -100 && tempKey != checkPointkeyCode()) {
             pointerReleased(x, y);
             clearInput();
             blDragged = true;
             iDraggedIndex = 0;
         } else {
             if (blDragged && iDraggedIndex >= 10) {
                 pointerPressed(x, y);
                 blDragged = false;
                 iDraggedIndex = -1;
             }
         }
     }

     final protected void pointerPressed(int x, int y) {
         pointerX = x;
         pointerY = y;
         onPointer = 0;
         checkPoint(x, y);
     }

     public int checkPointkeyCode() {
         switch (MainLogic.getInstance().byGameState) {
             case Constant.GAME_UI_ROAM:
                 int startY = Constant.UI_START_Y + Constant.DRAW_HEIGHT - 85;
                 if (pointerPressedLogic(52, startY - 162, 58, 80, true)) {//上380
                     return Constant.KEY_DIR_UP;
                 } else if (pointerPressedLogic(52, startY - 82, 58, 80, true)) {//下460
                     return Constant.KEY_DIR_DOWN;
                 } else if (pointerPressedLogic(2, startY - 112, 80, 58, true)) {//左430
                     keyPressed2(Constant.KEY_DIR_LEFT);
                     return Constant.KEY_DIR_LEFT;
                 } else if (pointerPressedLogic(82, startY - 112, 80, 58, true)) {//右430
                     return Constant.KEY_DIR_RIGHT;
                 }
                 break;
         }
         return tempKey;
     }

     public void checkPoint(int x, int y) {
         if (MainLogic.getInstance().uiInterFace.sysMessage.bShowPopUpMessage
                 || MainLogic.getInstance().uiInterFace.sysMessage.bShowNotice) {
             if (pointerPressedLogic(0, 583, 100, 57, false)) {//左软键
 //                keyPressed2(Constant.KEY_SOFT_LEFT);
                 keyPressed2(Constant.KEY_SOFT_RIGHT);
             } else if (pointerPressedLogic(260, 583, 100, 57, false)) {//右软键
 //                keyPressed2(Constant.KEY_SOFT_RIGHT);
                 keyPressed2(Constant.KEY_SOFT_LEFT);
             }
             return;
         } else if (MainLogic.getInstance().uiPackage.bShowBAG) {
        	 MainLogic.uiPackage.checkTouchPackage(x, y);
        	 
        	 
        	 checkSoftKey();
        	  System.out.println("bShowBAG State = " + MainLogic.getInstance().byGameState);
         } else {
         Integer key = new Integer(MainLogic.getInstance().byGameState); 
         System.out.println("State = " + MainLogic.getInstance().byGameState);
		switch (MainLogic.getInstance().byGameState) {	
		
		case Constant.GAME_UI_VIEW_EMAIL:
			if (MainLogic.uiEquipment != null) {
//				MainLogic.uiEquipment.updateViewEmail();
				MainLogic.uiEquipment.checkTouchViewEmail(x,y);	
            }
			break;
		
		case Constant.GAME_UI_OPERATION_EMAIL:
			if (MainLogic.uiEquipment != null) {
				MainLogic.uiEquipment.checkTouchOperationEmail(x,y);	
            }
			break;
			
			
		case Constant.GAME_UI_SEND_EMAIL:
			if (MainLogic.uiEquipment != null) {
				MainLogic.uiEquipment.checkTouchSendEmail(x,y);	
            }
			break;
		case Constant.GAME_UI_RECEIVE_EMAIL:
			if (MainLogic.uiEquipment != null) {
				MainLogic.uiEquipment.checkTouchReceiveEmail(x,y);	
            }
			break;
		case Constant.GAME_UI_TRANSFER_LIST:
			MainLogic.uiInterFace.npcTalk.checkTouchTransferList(x,y);			
			break;
		case Constant.GAME_UI_REVIVE:
			MainLogic.uiInterFace.checkTouchRevive(x,y);
			break;
		case Constant.GAME_UI_CHANGE_PASSWD://Yujiang 有个bug,在某些情况下这个界面没弹出，但是状态还是这个。
			pressBack();
			break;
		case Constant.GAME_UI_WAREHOUSE:
			 UiInterFace.store.checkTouchWareHouse(x,y);
			break;
		case Constant.GAME_UI_ACCEPT_TASK:
			MainLogic.uiInterFace.npcTalk.checkTouchTask(x,y);	
			break;
		case Constant.GAME_UI_STORE_BUY:
			 UiInterFace.store.checkTouchStoreBuy(x,y);
			break;
		case Constant.GAME_UI_LOOK_OTHERPLAYER:
			MainLogic.uiInterFace.checkTouchOtherPlayerLooked(x, y);
			break;		
		case Constant.GAME_UI_NEAR_PLAYER_LIST:
			MainLogic.uiInterFace.checkTouchNearPlayerList(x, y);
			break;
		case Constant.GAME_UI_PACKAGE:
			MainLogic.uiPackage.checkTouchPackage(x, y);
			break;
		case Constant.GAME_UI_PET_COMPOSE:
			UiInterFace.pet.checkTouchPetCompose(x, y);
			break;
		case Constant.GAME_UI_PET_POWER_STRENGTHEN:
			UiInterFace.pet.checkTouchPetPowerStrength(x, y);		
			break;	
		case Constant.GAME_UI_PET_EQUIP:
			UiInterFace.pet.checkTouchPetEquip(x, y);
			break;
		case Constant.GAME_UI_FRIEND_LIST:
			UiInterFace.friend.checkTouchFriendList(x,y);
			break;
		case Constant.GAME_UI_GAMESET:
			 MainLogic.getInstance().uiEquipment.checkTouchGameset(x, y);
			break;
		case Constant.GAME_UI_PLAYER_TOP:
			UiInterFace.property.checkTouchPlayerTop(x, y);
			break;
		case Constant.GAME_UI_HANGUP:		
			UiInterFace.property.checkTouchHangUp(x, y);
			break;
		 case Constant.GAME_UI_SKILL:
			 MainLogic.getInstance().uiEquipment.checkSkillTouch(x, y);
			 break;
		case Constant.GAME_UI_EQUIPMENT:
			MainLogic.getInstance().uiPackage.checkTouchEquip(x, y);
			break;
		case Constant.GAME_UI_PROPERTY_ADD_POINT:
			UiInterFace.property.checkTouchADDPoint(x, y);
			break;
		case Constant.GAME_UI_PLAYER_INFO:
			 UiInterFace.property.checkTouchPlayerInfo(x, y);
			break;
		case Constant.GAME_UI_MAP_WORLD:
			 MainLogic.getInstance().uiEquipment.checkTouchMapWorld(x, y);
			
			break;
		case Constant.GAME_UI_NPC_TALK:
			MainLogic.getInstance().uiInterFace.npcTalk.checkTouchNPCTalk(x, y);
			break;
		case Constant.GAME_UI_CREATE_ROLE:
			 MainLogic.getInstance().uiLogin.checkTouchCreateRole(x, y);
			break;
		case Constant.GAME_UI_SET:
			MainLogic.getInstance().uiLogin.checkTouchSet(x, y);
			break;
		case Constant.GAME_UI_ROAM:
			if (MainLogic.getInstance().uiInterFace.sysMessage.bShowGuide) {
				if (pointerPressedLogic(Constant.UI_START_X
						+ (Constant.DRAW_WIDTH - 196 >> 1), Constant.UI_START_Y
						+ Constant.DRAW_HEIGHT - 53, 49, 49, false)) {// 上
					keyPressed2(Constant.KEY_DIR_UP);
					KEY_BOTTOM = Constant.UP;
				} else if (pointerPressedLogic(Constant.UI_START_X
						+ (Constant.DRAW_WIDTH - 196 >> 1) + 49,
						Constant.UI_START_Y + Constant.DRAW_HEIGHT - 53, 49,
						49, false)) {// 下
					keyPressed2(Constant.KEY_DIR_DOWN);
					KEY_BOTTOM = Constant.DOWN;
				} else if (pointerPressedLogic(Constant.UI_START_X
						+ (Constant.DRAW_WIDTH - 196 >> 1) + 98,
						Constant.UI_START_Y + Constant.DRAW_HEIGHT - 53, 49,
						49, false)) {// 左
					keyPressed2(Constant.KEY_DIR_LEFT);
					KEY_BOTTOM = Constant.LEFT;
				} else if (pointerPressedLogic(Constant.UI_START_X
						+ (Constant.DRAW_WIDTH - 196 >> 1) + 147,
						Constant.UI_START_Y + Constant.DRAW_HEIGHT - 53, 49,
						49, false)) {// 右
					keyPressed2(Constant.KEY_DIR_RIGHT);
					KEY_BOTTOM = Constant.RIGHT;
				} else if (pointerPressedLogic(0, 583, 100, 57, false)) {// 左软键
					keyPressed2(Constant.KEY_SOFT_RIGHT);
				} else if (pointerPressedLogic(260, 583, 100, 57, false)) {// 右软键
					keyPressed2(Constant.KEY_SOFT_LEFT);
				}
				return;
			}
			int quickStartX = Constant.UI_START_X
					+ (Constant.DRAW_WIDTH - 350 >> 1);
			int quickStartY = Constant.UI_START_Y + Constant.DRAW_HEIGHT - 39;
			int startX = Constant.UI_START_X + (Constant.DRAW_WIDTH - 354 >> 1);
			int startY = quickStartY - 46;
			touch_y = startY;
			if (pointerPressedLogic(startX, startY, 70, 35, false)) {// 菜单
				keyPressed2(Constant.KEY_SOFT_LEFT);
			} else if (pointerPressedLogic(startX + 70, startY, 70, 35, false)) {// 任务
				GameBean.BY_PRE_GAME_UI_STATE = -1;
				MainLogic.getInstance().uiInterFace.npcTalk.openTaskList();
			} else if (pointerPressedLogic(startX + 140, startY, 70, 35, false)) {// 聊天
				MainLogic.getInstance().pushState(Constant.GAME_UI_CHAT, true);
			} else if (pointerPressedLogic(startX + 210, startY, 70, 35, false)) {// 目标
				keyPressed2(Constant.KEY_0);
			} else if (pointerPressedLogic(startX + 280, startY, 70, 35, false)) {// 确定
				keyPressed2(Constant.KEY_DIR_CENTER);
			} else if (pointerPressedLogic(52, startY - 162, 58, 80, true)) {// 上380
				keyPressed2(Constant.KEY_DIR_UP);
				KEY = Constant.UP;
			} else if (pointerPressedLogic(52, startY - 82, 58, 80, true)) {// 下460
				keyPressed2(Constant.KEY_DIR_DOWN);
				KEY = Constant.DOWN;
			} else if (pointerPressedLogic(2, startY - 112, 80, 58, true)) {// 左430
				keyPressed2(Constant.KEY_DIR_LEFT);
				KEY = Constant.LEFT;
			} else if (pointerPressedLogic(82, startY - 112, 80, 58, true)) {// 右430
				keyPressed2(Constant.KEY_DIR_RIGHT);
				KEY = Constant.RIGHT;
			} else if (pointerPressedLogic(quickStartX, quickStartY, 38, 38,
					false)) {// 快捷键1
				keyPressed2(Constant.KEY_1);
			} else if (pointerPressedLogic(quickStartX + 39, quickStartY, 38,
					38, false)) {// 快捷键2
				keyPressed2(Constant.KEY_2);
			} else if (pointerPressedLogic(quickStartX + 78, quickStartY, 38,
					38, false)) {// 快捷键3
				keyPressed2(Constant.KEY_3);
			} else if (pointerPressedLogic(quickStartX + 117, quickStartY, 38,
					38, false)) {// 快捷键4
				keyPressed2(Constant.KEY_4);
			} else if (pointerPressedLogic(quickStartX + 156, quickStartY, 38,
					38, false)) {// 快捷键5
				keyPressed2(Constant.KEY_5);
			} else if (pointerPressedLogic(quickStartX + 195, quickStartY, 38,
					38, false)) {// 快捷键6
				keyPressed2(Constant.KEY_6);
			} else if (pointerPressedLogic(quickStartX + 234, quickStartY, 38,
					38, false)) {// 快捷键7
				keyPressed2(Constant.KEY_7);
			} else if (pointerPressedLogic(quickStartX + 273, quickStartY, 38,
					38, false)) {// 快捷键8
				keyPressed2(Constant.KEY_8);
			} else if (pointerPressedLogic(quickStartX + 312, quickStartY, 38,
					38, false)) {// 快捷键9
				keyPressed2(Constant.KEY_9);
			}

			break;
		case Constant.GAME_UI_ROAM_MENU:
			if (pointerPressedLogic(Constant.UI_START_X
					+ (Constant.DRAW_WIDTH - 196 >> 1), Constant.UI_START_Y
					+ Constant.DRAW_HEIGHT - 53, 49, 49, false)) {// 上
				keyPressed2(Constant.KEY_DIR_UP);
				KEY_BOTTOM = Constant.UP;
			} else if (pointerPressedLogic(Constant.UI_START_X
					+ (Constant.DRAW_WIDTH - 196 >> 1) + 49,
					Constant.UI_START_Y + Constant.DRAW_HEIGHT - 53, 49, 49,
					false)) {// 下
				keyPressed2(Constant.KEY_DIR_DOWN);
				KEY_BOTTOM = Constant.DOWN;
			} else if (pointerPressedLogic(Constant.UI_START_X
					+ (Constant.DRAW_WIDTH - 196 >> 1) + 98,
					Constant.UI_START_Y + Constant.DRAW_HEIGHT - 53, 49, 49,
					false)) {// 左
				keyPressed2(Constant.KEY_DIR_LEFT);
				KEY_BOTTOM = Constant.LEFT;
			} else if (pointerPressedLogic(Constant.UI_START_X
					+ (Constant.DRAW_WIDTH - 196 >> 1) + 147,
					Constant.UI_START_Y + Constant.DRAW_HEIGHT - 53, 49, 49,
					false)) {// 右
				keyPressed2(Constant.KEY_DIR_RIGHT);
				KEY_BOTTOM = Constant.RIGHT;
			} else if (pointerPressedLogic(0, 583, 100, 57, false)) {// 左软键
				// keyPressed2(Constant.KEY_SOFT_LEFT);
				keyPressed2(Constant.KEY_SOFT_RIGHT);
			} else if (pointerPressedLogic(260, 583, 100, 57, false)) {// 右软键
				// keyPressed2(Constant.KEY_SOFT_RIGHT);
				keyPressed2(Constant.KEY_SOFT_LEFT);
			} else if (pointerPressedLogic(
					0,
					Constant.UI_START_Y
							+ (Constant.DRAW_HEIGHT
									- 57
									- MainLogic.getInstance().uiInterFace.menuHeight >> 1),
					Constant.DRAW_WIDTH,
					MainLogic.getInstance().uiInterFace.menuH, false)) {// 数字键1
				
					////////////////////////////
					//YuJiang 排行二级菜单touchStartY
					int payTopTouchstartY = 144;
					UiInterFace.property.setPlayTopTouchStartY(payTopTouchstartY);
					//////////////////////////////
				keyPressed2(Constant.KEY_1);
			} else if (pointerPressedLogic(
					0,
					Constant.UI_START_Y
							+ (Constant.DRAW_HEIGHT
									- 57
									- MainLogic.getInstance().uiInterFace.menuHeight >> 1)
							+ MainLogic.getInstance().uiInterFace.menuH,
					Constant.DRAW_WIDTH,
					MainLogic.getInstance().uiInterFace.menuH, false)) {// 数字键2
				////////////////////////////
			//YuJiang 排行二级菜单touchStartY
			int payTopTouchstartY = 175;
			UiInterFace.property.setPlayTopTouchStartY(payTopTouchstartY);
			//////////////////////////////
				keyPressed2(Constant.KEY_2);
			} else if (pointerPressedLogic(
					0,
					Constant.UI_START_Y
							+ (Constant.DRAW_HEIGHT
									- 57
									- MainLogic.getInstance().uiInterFace.menuHeight >> 1)
							+ MainLogic.getInstance().uiInterFace.menuH * 2,
					Constant.DRAW_WIDTH,
					MainLogic.getInstance().uiInterFace.menuH, false)) {// 数字键3
			////////////////////////////
			//YuJiang 排行二级菜单touchStartY
			int payTopTouchstartY = 175;
			UiInterFace.property.setPlayTopTouchStartY(payTopTouchstartY);
			//////////////////////////////
				keyPressed2(Constant.KEY_3);
			} else if (pointerPressedLogic(
					0,
					Constant.UI_START_Y
							+ (Constant.DRAW_HEIGHT
									- 57
									- MainLogic.getInstance().uiInterFace.menuHeight >> 1)
							+ MainLogic.getInstance().uiInterFace.menuH * 3,
					Constant.DRAW_WIDTH,
					MainLogic.getInstance().uiInterFace.menuH, false)) {// 数字键4
			////////////////////////////
			//YuJiang 排行二级菜单touchStartY
			int payTopTouchstartY = 196;
			UiInterFace.property.setPlayTopTouchStartY(payTopTouchstartY);
			//////////////////////////////
				keyPressed2(Constant.KEY_4);
			} else if (pointerPressedLogic(
					0,
					Constant.UI_START_Y
							+ (Constant.DRAW_HEIGHT
									- 57
									- MainLogic.getInstance().uiInterFace.menuHeight >> 1)
							+ MainLogic.getInstance().uiInterFace.menuH * 4,
					Constant.DRAW_WIDTH,
					MainLogic.getInstance().uiInterFace.menuH, false)) {// 数字键5
				keyPressed2(Constant.KEY_5);
			} else if (pointerPressedLogic(
					0,
					Constant.UI_START_Y
							+ (Constant.DRAW_HEIGHT
									- 57
									- MainLogic.getInstance().uiInterFace.menuHeight >> 1)
							+ MainLogic.getInstance().uiInterFace.menuH * 5,
					Constant.DRAW_WIDTH,
					MainLogic.getInstance().uiInterFace.menuH, false)) {// 数字键6
				keyPressed2(Constant.KEY_6);
			} else if (pointerPressedLogic(
					0,
					Constant.UI_START_Y
							+ (Constant.DRAW_HEIGHT
									- 57
									- MainLogic.getInstance().uiInterFace.menuHeight >> 1)
							+ MainLogic.getInstance().uiInterFace.menuH * 6,
					Constant.DRAW_WIDTH,
					MainLogic.getInstance().uiInterFace.menuH, false)) {// 数字键7
				keyPressed2(Constant.KEY_7);
			} else if (pointerPressedLogic(
					0,
					Constant.UI_START_Y
							+ (Constant.DRAW_HEIGHT
									- 57
									- MainLogic.getInstance().uiInterFace.menuHeight >> 1)
							+ MainLogic.getInstance().uiInterFace.menuH * 7,
					Constant.DRAW_WIDTH,
					MainLogic.getInstance().uiInterFace.menuH, false)) {// 数字键8
				keyPressed2(Constant.KEY_8);
			} else if (pointerPressedLogic(
					0,
					Constant.UI_START_Y
							+ (Constant.DRAW_HEIGHT
									- 57
									- MainLogic.getInstance().uiInterFace.menuHeight >> 1)
							+ MainLogic.getInstance().uiInterFace.menuH * 8,
					Constant.DRAW_WIDTH,
					MainLogic.getInstance().uiInterFace.menuH, false)) {// 数字键9
				keyPressed2(Constant.KEY_9);
			} else if (pointerPressedLogic(
					0,
					Constant.UI_START_Y
							+ (Constant.DRAW_HEIGHT
									- 57
									- MainLogic.getInstance().uiInterFace.menuHeight >> 1)
							+ MainLogic.getInstance().uiInterFace.menuH * 9,
					Constant.DRAW_WIDTH,
					MainLogic.getInstance().uiInterFace.menuH, false)) {// *
				keyPressed2(Constant.KEY_STAR);
			} else if (pointerPressedLogic(
					0,
					Constant.UI_START_Y
							+ (Constant.DRAW_HEIGHT
									- 57
									- MainLogic.getInstance().uiInterFace.menuHeight >> 1)
							+ MainLogic.getInstance().uiInterFace.menuH * 10,
					Constant.DRAW_WIDTH,
					MainLogic.getInstance().uiInterFace.menuH, false)) {// 右 1
				MainLogic.getInstance().uiInterFace.bUiKeyPressLock = true;
				if (MainLogic.getInstance().uiInterFace.iSelectIndex == 11) {
					MainLogic.getInstance().uiInterFace.byLeft = 2;
					MainLogic.getInstance().uiInterFace.RoamMenuClickOk(1);
					return;
				} else if (MainLogic.getInstance().uiInterFace.menu.length >= 11) {
					int temp = MainLogic.getInstance().uiInterFace.iSelectIndex > 11 ? 2
							: 1;
					MainLogic.getInstance().uiInterFace.iSelectIndex = 11;
					startY = Constant.UI_START_Y
							+ (Constant.DRAW_HEIGHT
									- 57
									- MainLogic.getInstance().uiInterFace.menuHeight >> 1)
							+ (MainLogic.getInstance().uiInterFace.iSelectIndex - 1)
							* MainLogic.getInstance().uiInterFace.menuH;
					MainLogic.getInstance().uiInterFace.iArray = new int[] {
							temp, 1, startY, 2, 50, 5 };
				}
			} else if (pointerPressedLogic(
					0,
					Constant.UI_START_Y
							+ (Constant.DRAW_HEIGHT
									- 57
									- MainLogic.getInstance().uiInterFace.menuHeight >> 1)
							+ MainLogic.getInstance().uiInterFace.menuH * 11,
					Constant.DRAW_WIDTH,
					MainLogic.getInstance().uiInterFace.menuH, false)) {// 右 2
				MainLogic.getInstance().uiInterFace.bUiKeyPressLock = true;
				if (MainLogic.getInstance().uiInterFace.iSelectIndex == 12) {
					MainLogic.getInstance().uiInterFace.byLeft = 2;
					MainLogic.getInstance().uiInterFace.RoamMenuClickOk(2);
					return;
				} else if (MainLogic.getInstance().uiInterFace.menu.length >= 12) {
					int temp = MainLogic.getInstance().uiInterFace.iSelectIndex > 12 ? 2
							: 1;
					MainLogic.getInstance().uiInterFace.iSelectIndex = 12;
					startY = Constant.UI_START_Y
							+ (Constant.DRAW_HEIGHT
									- 57
									- MainLogic.getInstance().uiInterFace.menuHeight >> 1)
							+ (MainLogic.getInstance().uiInterFace.iSelectIndex - 1)
							* MainLogic.getInstance().uiInterFace.menuH;
					MainLogic.getInstance().uiInterFace.iArray = new int[] {
							temp, 1, startY, 2, 50, 5 };
				}
			} else if (pointerPressedLogic(
					0,
					Constant.UI_START_Y
							+ (Constant.DRAW_HEIGHT
									- 57
									- MainLogic.getInstance().uiInterFace.menuHeight >> 1)
							+ MainLogic.getInstance().uiInterFace.menuH * 12,
					Constant.DRAW_WIDTH,
					MainLogic.getInstance().uiInterFace.menuH, false)) {// 右 3
				MainLogic.getInstance().uiInterFace.bUiKeyPressLock = true;
				if (MainLogic.getInstance().uiInterFace.iSelectIndex == 13) {
					MainLogic.getInstance().uiInterFace.byLeft = 2;
					MainLogic.getInstance().uiInterFace.RoamMenuClickOk(3);
					return;
				} else if (MainLogic.getInstance().uiInterFace.menu.length >= 13) {
					int temp = MainLogic.getInstance().uiInterFace.iSelectIndex > 13 ? 2
							: 1;
					MainLogic.getInstance().uiInterFace.iSelectIndex = 13;
					startY = Constant.UI_START_Y
							+ (Constant.DRAW_HEIGHT
									- 57
									- MainLogic.getInstance().uiInterFace.menuHeight >> 1)
							+ (MainLogic.getInstance().uiInterFace.iSelectIndex - 1)
							* MainLogic.getInstance().uiInterFace.menuH;
					MainLogic.getInstance().uiInterFace.iArray = new int[] {
							temp, 1, startY, 2, 50, 5 };
				}
			} else if (pointerPressedLogic(
					0,
					Constant.UI_START_Y
							+ (Constant.DRAW_HEIGHT
									- 57
									- MainLogic.getInstance().uiInterFace.menuHeight >> 1)
							+ MainLogic.getInstance().uiInterFace.menuH * 13,
					Constant.DRAW_WIDTH,
					MainLogic.getInstance().uiInterFace.menuH, false)) {// 右 4
				MainLogic.getInstance().uiInterFace.bUiKeyPressLock = true;
				if (MainLogic.getInstance().uiInterFace.iSelectIndex == 14) {
					MainLogic.getInstance().uiInterFace.byLeft = 2;
					MainLogic.getInstance().uiInterFace.RoamMenuClickOk(4);
					return;
				} else if (MainLogic.getInstance().uiInterFace.menu.length >= 14) {
					int temp = MainLogic.getInstance().uiInterFace.iSelectIndex > 14 ? 2
							: 1;
					MainLogic.getInstance().uiInterFace.iSelectIndex = 14;
					startY = Constant.UI_START_Y
							+ (Constant.DRAW_HEIGHT
									- 57
									- MainLogic.getInstance().uiInterFace.menuHeight >> 1)
							+ (MainLogic.getInstance().uiInterFace.iSelectIndex - 1)
							* MainLogic.getInstance().uiInterFace.menuH;
					MainLogic.getInstance().uiInterFace.iArray = new int[] {
							temp, 1, startY, 2, 50, 5 };
				}
			}
			break;
		case Constant.GAME_UI_TONG_MENU:
//		case Constant.GAME_UI_NPC_TALK:
		case Constant.GAME_UI_EQUIP_REPAIR:
		case Constant.GAME_UI_TASK_AUTO_FIND_PATH:
		case Constant.GAME_UI_TEAM_ANSWER:
		//Yujiang
//		case Constant.GAME_UI_WAREHOUSE:
//		case Constant.GAME_UI_REVIVE:
		
		case Constant.GAME_UI_VIEW_OTHER_PLAYER:
//		case Constant.GAME_UI_MAP_WORLD:
//		case Constant.GAME_UI_QUICK_OPTION:
			if (pointerPressedLogic(Constant.UI_START_X
					+ (Constant.DRAW_WIDTH - 196 >> 1), Constant.UI_START_Y
					+ Constant.DRAW_HEIGHT - 53, 49, 49, false)) {// 上
				keyPressed2(Constant.KEY_DIR_UP);
				KEY_BOTTOM = Constant.UP;
			} else if (pointerPressedLogic(Constant.UI_START_X
					+ (Constant.DRAW_WIDTH - 196 >> 1) + 49,
					Constant.UI_START_Y + Constant.DRAW_HEIGHT - 53, 49, 49,
					false)) {// 下
				keyPressed2(Constant.KEY_DIR_DOWN);
				KEY_BOTTOM = Constant.DOWN;
			} else if (pointerPressedLogic(Constant.UI_START_X
					+ (Constant.DRAW_WIDTH - 196 >> 1) + 98,
					Constant.UI_START_Y + Constant.DRAW_HEIGHT - 53, 49, 49,
					false)) {// 左
				keyPressed2(Constant.KEY_DIR_LEFT);
				KEY_BOTTOM = Constant.LEFT;
			} else if (pointerPressedLogic(Constant.UI_START_X
					+ (Constant.DRAW_WIDTH - 196 >> 1) + 147,
					Constant.UI_START_Y + Constant.DRAW_HEIGHT - 53, 49, 49,
					false)) {// 右
				keyPressed2(Constant.KEY_DIR_RIGHT);
				KEY_BOTTOM = Constant.RIGHT;
			} else if (pointerPressedLogic(0, 583, 100, 57, false)) {// 左软键
				// keyPressed2(Constant.KEY_SOFT_LEFT);
				keyPressed2(Constant.KEY_SOFT_RIGHT);
			} else if (pointerPressedLogic(260, 583, 100, 57, false)) {// 右软键
				// keyPressed2(Constant.KEY_SOFT_RIGHT);
				keyPressed2(Constant.KEY_SOFT_LEFT);
			}
			break;
		case Constant.GAME_UI_OPTION_LIST:
			 MainLogic.getInstance().uiLogin.checkTouchOptionList(x, y);
			// if (pointerPressedLogic(65, 460, 60, 60, false)) {//左
			// keyPressed2(Constant.KEY_DIR_LEFT);
			// } else if (pointerPressedLogic(265, 460, 60, 60, false)) {//右
			// keyPressed2(Constant.KEY_DIR_RIGHT);
			// } else if (pointerPressedLogic(130, 510, 100, 100, false)) {//确定
			// keyPressed2(Constant.KEY_DIR_CENTER);
			// KEY = Constant.FIRE;
			// }
			break;
		case Constant.GAME_UI_LOGIN:
			MainLogic.getInstance().uiLogin.checkTouchLogin(x, y);
			break;
		case Constant.GAME_UI_GAMESET_QUICKEYS:
			UiInterFace.gameSet.checkTouchQuickSet(x, y);
			break;
		case Constant.GAME_UI_MERIDIANS:
			MainLogic.getInstance().uiEquipment.checkTouchMeridians(x, y);
			break;
        case Constant.GAME_UI_TASK_LIST:
            MainLogic.getInstance().uiInterFace.npcTalk.checkTouchTaskList(x, y);
            break;
        case Constant.GAME_UI_CHAT:
            MainLogic.getInstance().uiEquipment.checkTouchChat(x, y);
        case Constant.GAME_UI_QUICK_OPTION:
        	break;
		default:
			checkSoftKey();
			break;
		}
         }
     }

     final protected void pointerReleased(int x, int y) {
         pointerX = x;
         pointerY = y;
         keyReleased2(tempKey);
         onPointer = -100;
         KEY = -100;
         KEY_BOTTOM = -100;
         tempKey = -100;
 //        System.out.println("pointerReleasedX  ::: " + pointerX + "   pointerReleasedY  ::: " + pointerY);
     }

     public static final int getPointerLogicX() {
         return (pointerX);
     }

     public static final int getPointerLogicY() {
         return (pointerY);
     }
    //#endif

    /**
     * 在手机屏幕打印内存和fps信息（流量信息）
     */
    private final void drawBenchMark(Graphics g) {
//        long now = System.currentTimeMillis();
//        if (now - this.lastSystemTime >= 1000) {
//            NetManager.testNums = 0;
//            this.fps = this.additiveFrameCount;
//            this.lastSystemTime = now;
//            this.additiveFrameCount = 0;
//        }
//        this.additiveFrameCount += 1;
//        g.setColor(0xff0000);
        //#if NOKIAN73
//#         g.drawString("  ", 0, 60, Graphics.TOP | Graphics.LEFT);
        //#endif
//        g.drawString("MSIDs:" + NetManager.testMsid, 0, 90, Graphics.TOP | Graphics.LEFT);
//        g.drawString("MSIDr:" + NetManager.testMsid2, 0, 120, Graphics.TOP | Graphics.LEFT);
//        g.drawString("ToTAL:" + this.rt.totalMemory() / 1024, 0, 140, Graphics.TOP | Graphics.LEFT);
//        g.drawString("USED(k):" + (this.rt.totalMemory() - this.rt.freeMemory()) / 1024, 0, 160, Graphics.TOP | Graphics.LEFT);
//        g.drawString("recv:" + this.iReceiveCounter, 0, 180, 20);
//        if (Constant.bTestFlow) {
//            //g.drawString("flow:" + Tools.byteConvert(MainLogic.lFlowCount), 0, 180, Graphics.TOP | Graphics.LEFT);
//        }
//        g.drawString("  " + key111, 0, 70, Graphics.TOP | Graphics.LEFT);
    }

    /**
     * 绘制日志和方法堆栈
     * @param g Graphics 画笔
     */
    private static final void printLogAndStack(Graphics g) {
        //background
        g.setColor(0x00ffffff);
        g.setClip(0, 0, Constant.SCREEN_WIDTH, Constant.SCREEN_HEIGHT);
        g.fillRect(0, 0, Constant.SCREEN_WIDTH, Constant.SCREEN_HEIGHT);
        //log info
        int dy = 0;
        if (logInfo != null && logInfo.size() > 0) {
            g.setColor(0x000000ff);
            g.setClip(0, 0, Constant.SCREEN_WIDTH, Constant.SCREEN_HEIGHT);
            for (int i = 0; i
                    < logInfo.size(); i++) {
                g.drawString((String) logInfo.elementAt(i), screenInfoPosX, screenInfoPosY + i * Font.getDefaultFont().getHeight(), Graphics.TOP | Graphics.LEFT);
                dy += Font.getDefaultFont().getHeight();
            }
            g.setClip(0, 0, Constant.SCREEN_WIDTH, Constant.SCREEN_HEIGHT);
        }

        //call stack
        if (callStack != null && callStack.size() > 0) {
            g.setColor(0x00ff0000);
            g.setClip(0, 0, Constant.SCREEN_WIDTH, Constant.SCREEN_HEIGHT);
            for (int i = 0; i < callStack.size(); i++) {
                g.drawString((String) callStack.elementAt(callStack.size() - i - 1), i == 0 ? screenInfoPosX : screenInfoPosX + 20, screenInfoPosY + dy + i * Font.getDefaultFont().getHeight(), Graphics.TOP | Graphics.LEFT);
            }
            g.setClip(0, 0, Constant.SCREEN_WIDTH, Constant.SCREEN_HEIGHT);
        }
    }

    /**
     * 方法名入栈
     * @param methodName String 方法名
     */
    public synchronized static final void pushCallStack(String methodName) {
        if (callStack == null) {
            callStack = new Vector();
        }
        callStack.addElement(methodName);
    }

    /**
     * 方法名出栈
     */
    public synchronized static final void popCallStack() {
        if (callStack != null && callStack.size() > 0) {
            callStack.removeElementAt(callStack.size() - 1);
        }
    }

    /**
     * 添加日志
     * @param log String 日志信息
     */
    public synchronized static final void addLog(String log) {
        final int CAPACITY = 250;
        if (logInfo == null) {
            logInfo = new Vector(CAPACITY);
        } else if (logInfo.size() == CAPACITY) {
            Vector v = new Vector(CAPACITY);
            for (int i = CAPACITY / 2; i
                    < CAPACITY; i++) {
                v.addElement(logInfo.elementAt(i));
            }
            logInfo = v;
            v = null;
        } //        Tools.printLog(log);
        logInfo.addElement(log);
    }

    /**
     * 游戏主线程
     */
    public void run() {
        while (!this.blStopGame) {
            try {
//                if (Constant.DEBUG) {
//                    if (printStackToScreen) {
//                        if (this.keyPressedLogic(Constant.GAME_KEY_LEFT, true)) {
//                            screenInfoPosX += 28;
//                        } else if (this.keyPressedLogic(Constant.GAME_KEY_RIGHT, true)) {
//                            screenInfoPosX -= 28;
//                        } else if (this.keyPressedLogic(Constant.GAME_KEY_UP, true)) {
//                            screenInfoPosY += 28;
//                        } else if (this.keyPressedLogic(Constant.GAME_KEY_DOWN, true)) {
//                            screenInfoPosY -= 28;
//                        } else if (this.keyPressedLogic(Constant.GAME_KEY_STAR, false)) {
//                            screenInfoPosX = 0;
//                            screenInfoPosY = 0;
//                            callStack = null;
//                            printStackToScreen = false;
//                            clearInput();
//                        } else if (this.keyPressedLogic(Constant.GAME_KEY_0, false)) {
//                            logInfo = null;
//                            clearInput();
//                        } else if (this.keyPressedLogic(Constant.GAME_KEY_POUND, false)) {
//                            printStackToScreen = false;
//                            clearInput();
//                        }
//                        repaint();
//                        serviceRepaints();
//                        Thread.sleep(50);
//                        continue;
//                    } else if (this.keyPressedLogic(Constant.GAME_KEY_STAR, false)) {
//                        printStackToScreen = true;
//                    }
//                }
                lStartTime = System.currentTimeMillis();
                if (lStartTime - lTimegc > 10000) {
                    lTimegc = System.currentTimeMillis();
                    System.gc();
                }
                MainLogic.lClientCurrentTime = lStartTime - MainLogic.lCSSpaceTime;
                // 更新游戏数据
                this.updateGame();
                //锁帧，休息线程
                lTimeTaken = System.currentTimeMillis() - lStartTime;
                if (lTimeTaken < Constant.MILLIS_PER_TICK) {
                    Thread.currentThread().sleep(Constant.MILLIS_PER_TICK - lTimeTaken);
                } else {
                    Thread.currentThread().yield();
                }
            } catch (Exception e) {
                if (Constant.DEBUG) {
                    e.printStackTrace();
//          MainFrame.addLog("游戏主线程内出现异常！" + e.toString());
//                    this.printStackToScreen = true;
                }
            }
        }
        //结束游戏
        if (this.blStopGame) {
            this.clearRes();
        }
    }
    
    public static HashMap s_uiTouch = new HashMap();
    
    public static void clearUI(int gamestate) {
    	Integer key = new Integer(gamestate);
    	s_uiTouch.remove(key);
    }
    
    public static boolean existUI(int gamestate) {
    	Integer key = new Integer(gamestate);
    	return s_uiTouch.get(key) != null;
    }
    
    public static void putHash(int gamestate, int[][] touchrect) {
    	Integer key = new Integer(gamestate);
//    	return s_uiTouch.get(key) != null;
    	s_uiTouch.put(key, touchrect);
    }
    
    public static int checkTouch(int x, int y, int[][] touch) {
    	int touched = -1;
    	for (int i=0;i< touch.length;i++) {
    		int[] rect = touch[i];
    		if (rect != null && pointerPressedLogic(rect[0], rect[1], rect[2], rect[3], false)) {
    			touched = i;
    			break;
    		}
    	}
    	return touched;
    }
    
    public static void pressBack() {
    	keyPressed2(Constant.KEY_SOFT_RIGHT);
//    	KEY = Constant.NEGATIVE;
    }
    
    public static void pressFire() {
		keyPressed2(Constant.KEY_DIR_CENTER);
		KEY = Constant.FIRE;
    }
    
    /**0上,1下,2左,3右。
     * @param dir
     */
    public static void pressDir(int dir) {
    	switch(dir) {
    	case 0:
    		 keyPressed2(Constant.KEY_DIR_UP);
    		 KEY = Constant.UP;
    		break;
    	case 1:
    		 keyPressed2(Constant.KEY_DIR_DOWN);
    		 KEY = Constant.DOWN;
    		break;
    	case 2:
    		 keyPressed2(Constant.KEY_DIR_LEFT);
    		 KEY = Constant.LEFT;
    		break;
    	case 3:
    		 keyPressed2(Constant.KEY_DIR_RIGHT);
    		 KEY = Constant.RIGHT;
    		break;
    	}
    }
    /**
     * 数字键
     * @param num
     */
    public static void pressNum(int num)
    {
    	switch(num)
    	{
    	case 0:
    		keyPressed2(Constant.KEY_0);
    		KEY = Constant.GAME_KEY_0;
    		break;
    	case 1:
    		keyPressed2(Constant.KEY_1);
    		KEY = Constant.GAME_KEY_1;
    		break;
    	case 2:
    		keyPressed2(Constant.KEY_2);
    		KEY = Constant.GAME_KEY_2;
    		break;
    	case 3:
    		keyPressed2(Constant.KEY_3);
    		KEY = Constant.GAME_KEY_3;
    		break;
    	case 4:
    		keyPressed2(Constant.KEY_4);
    		KEY = Constant.GAME_KEY_4;
    		break;
    	case 5:
    		keyPressed2(Constant.KEY_5);
    		KEY = Constant.GAME_KEY_5;
    		break;
    	case 6:
    		keyPressed2(Constant.KEY_6);
    		KEY = Constant.GAME_KEY_6;
    		break;
    	case 7:
    		keyPressed2(Constant.KEY_7);
    		KEY = Constant.GAME_KEY_7;
    		break;
    	case 8:
    		keyPressed2(Constant.KEY_8);
    		KEY = Constant.GAME_KEY_8;
    		break;
    	case 9:
    		keyPressed2(Constant.KEY_9);
    		KEY = Constant.GAME_KEY_9;
    		break;
    		
    	}
    }
	public static void checkSoftKey() {
		// if (pointerPressedLogic(60, 410, 58, 80, false)) {//上
		// keyPressed2(Constant.KEY_DIR_UP);
		// KEY = Constant.UP;
		// } else if (pointerPressedLogic(60, 490, 58, 80, false)) {//下
		// keyPressed2(Constant.KEY_DIR_DOWN);
		// KEY = Constant.DOWN;
		// } else if (pointerPressedLogic(10, 460, 80, 58, false)) {//左
		// keyPressed2(Constant.KEY_DIR_LEFT);
		// KEY = Constant.LEFT;
		// } else if (pointerPressedLogic(90, 460, 80, 58, false)) {//右
		// keyPressed2(Constant.KEY_DIR_RIGHT);
		// KEY = Constant.RIGHT;
		// } else if (pointerPressedLogic(240, 445, 91, 91, false)) {//确定
		// keyPressed2(Constant.KEY_DIR_CENTER);
		// KEY = Constant.FIRE;
		// } else
		
		if (pointerPressedLogic(0, 583, 100, 57, false)) {// 左软键
			// keyPressed2(Constant.KEY_SOFT_LEFT);
			keyPressed2(Constant.KEY_SOFT_RIGHT);
		} else if (pointerPressedLogic(260, 583, 100, 57, false)) {// 右软键
			// keyPressed2(Constant.KEY_SOFT_RIGHT);
			keyPressed2(Constant.KEY_SOFT_LEFT);
		} else if (MainLogic.getInstance().byGameState == Constant.GAME_UI_ACCEPT_TASK
				&& pointerPressedLogic(15, 200, Constant.DRAW_WIDTH - 50,
						Constant.FONT_SMALL_HEIGHT + 15, false)) {// 左
			keyPressed2(Constant.KEY_0);
			KEY = Constant.KEY_0;
		}
	}
}
